/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class AnimatingLabel extends JLabel implements Animation {
	private int fps = 10;
	private boolean animating = false;
	private Icon[] images;
	private int frame=0;

	public AnimatingLabel(String[] p_animation,int p_fps)
	throws IOException{
		this(null,p_animation,p_fps);
	}

	public AnimatingLabel(String p_name,String[] p_animation,int p_fps)
	throws IOException{
		super(p_name);
		load(p_animation);
		fps=p_fps;
	}

	public void addNotify(){
		super.addNotify();
		if (images.length>1){
			AnimationThread.add(this);
		}
	}

	public void removeNotify(){
		super.removeNotify();
		if (images.length>1){
			AnimationThread.remove(this);
		}
	}

	private void load(String[] resource_name)
	throws IOException {
		ImageIcon[] loaded=new ImageIcon[resource_name.length];
		int width=0,height=0;
		Toolkit toolkit=Toolkit.getDefaultToolkit();
		for (int t=0;t<resource_name.length;t++){
			URL url=Thread.currentThread().getContextClassLoader().getResource(resource_name[t]);
			if (url==null){
				throw new IOException("Can not find resource ["+resource_name[t]+"]");
			}
			loaded[t]=new ImageIcon(toolkit.createImage(url));
			// Find max width
			if (loaded[t].getIconWidth()>width){
				width=loaded[t].getIconWidth();
			}
			// Find max height
			if (loaded[t].getIconHeight()>height){
				height=loaded[t].getIconHeight();
			}

		}
		// Build correct images
		BufferedImage buffered=new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D g=buffered.createGraphics();
		g.setColor(new Color(0,0,0,0));
		g.fillRect(0,0,width,height);
		images=new Icon[loaded.length];
		for (int t=0;t<loaded.length;t++){
			// Put loaded ontop of buffere image
			g.drawImage(loaded[t].getImage(),0,0,null);
			// Take out an image for this icon
			BufferedImage frame=new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
			frame.createGraphics().drawImage(buffered.getSubimage(0,0,width,height),0,0,null);
			images[t]=new ImageIcon(frame);
		}
		// Set first icon
		setIcon(images[0]);
	}

	public boolean isAnimating() {
		return true;
	}

	public int animationFPS() {
		return fps;
	}

	public void animate() {
		frame=(frame+1)%images.length;
		setIcon(images[frame]);
	}
}

